
import store from '../store'
declare global {
  interface Window {
    Electron: any
  }
}
const Electron = window.Electron;
const ipcRender = Electron?.ipcRenderer;
interface BackEvent {
  type: string,
  data: object
}

if (ipcRender) {
  // 接收后台相应你的事件
  ipcRender.on('back-event', (e:any, data:any) => {
    console.log('e: ', e, data, store)
    manageBackEvent(data);
  })
}


// 处理后台传递过来的事件
function manageBackEvent(e:BackEvent) {
  switch(e.type) {
    case 'refresh-win-state': {
      console.log('refresh-win-state: ', e, store);
      store.dispatch({
        type: 'REFRESH_WIN_STATE',
        data: e.data
      })
      break;
    }
  }
}

// 前台向后台发送事件
const sendEvent = (type:string, data:object = {}) => {
  const ipcRender = window.Electron.ipcRenderer;
  console.log('type: ', type, data);
  ipcRender.send('ui-event', {type, data})
}


export default {
  sendEvent
};
